local skel = fk.CreateSkill {
  name = "lb_zero__shouwu",
  max_branches_use_time = {
    ["round"] = {
      [Player.HistoryRound] = 1
    },
  }
}

Fk:loadTranslationTable {
  ["lb_zero__shouwu"] = "狩舞",
  [":lb_zero__shouwu"] = "每轮限一次，回合开始时，你可弃置任意张牌并选择等量不同项，你本轮下次执行选择项后视为使用一张【杀】：" ..
      "1.摸一张牌；2.造成1点伤害；3.回复1点体力；4.弃置场上一张牌。" ..
      "本轮结束时，若你执行过所有选择项，你依次执行之。",

  ["@[lb_zero__shouwu]lb_zero__shouwu-round"] = "狩舞",
  ["#lb_zero__shouwu_1"] = "摸一张牌",
  ["#lb_zero__shouwu_2"] = "造成1点伤害",
  ["#lb_zero__shouwu_3"] = "回复1点体力",
  ["#lb_zero__shouwu_4"] = "弃置场上一张牌",

  ["#lb_zero__shouwu"] = "狩舞：你可以弃置任意张牌并选择等量不同项，你本轮下次执行选择项后视为使用一张【杀】",
  ["#lb_zero__shouwu_damage"] = "狩舞：请分配1点伤害",
  ["#lb_zero__shouwu_choose"] = "狩舞：请弃置一名角色场上一张牌",
  ["#lb_zero__shouwu_dis"] = "狩舞：请弃置 %src 场上一张牌",

  ["$lb_zero__shouwu1"] = "狩猎时间。",
  ["$lb_zero__shouwu2"] = "我会咬紧你的咽喉！",
  ["$lb_zero__shouwu3"] = "我将为您扫平一切障碍。",
  ["$lb_zero__shouwu4"] = "就以这份力量。",
}

Fk:addQmlMark {
  name = "lb_zero__shouwu",
  how_to_show = function(name, value, player)
    if type(value) ~= "table" then return " " end
    local num_list = { "①", "②", "③", "④" }
    local mark = {}
    for _, val in ipairs(value) do
      local choice = num_list[val]
      if table.contains(player:getTableMark("lb_zero__shouwu-round"), val) then
        choice = "<font color='#0598BC'>" .. choice .. "</font></a>"
      end
      table.insert(mark, choice)
    end
    return #mark == 0 and tostring(#value) or table.concat(mark)
  end,
}

---@param player ServerPlayer
local can_shouwu = function(player, choice)
  return player:hasSkill(skel.name)
      and table.contains(player:getTableMark("@[lb_zero__shouwu]lb_zero__shouwu-round"), choice)
      and not table.contains(player:getTableMark("lb_zero__shouwu-round"), choice)
end

---@param player ServerPlayer
local use_shouwu = function(player, choice)
  local room = player.room
  room:notifySkillInvoked(player, skel.name, "offensive")
  player:broadcastSkillInvoke(skel.name, math.random(1, 2))
  room:addTableMarkIfNeed(player, "lb_zero__shouwu-round", choice)
  room:askToUseVirtualCard(player, {
    name = "slash",
    skill_name = skel.name,
    cancelable = false,
  })
end

skel:addEffect(fk.TurnStart, {
  audio_index = { 1, 2 },
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and not player:isNude()
        and player:usedSkillTimes(skel.name, Player.HistoryRound, "round") == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "#lb_zero__shouwu_active",
      prompt = "#lb_zero__shouwu",
      cancelable = true,
    })
    if success and dat then
      event:setCostData(self, { cards = dat.cards, choices = dat.interaction })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "round")
    local room = player.room
    local cost_data = event:getCostData(self)
    local cards = cost_data.cards
    room:throwCard(cards, skel.name, player, player)
    if player.dead then return end
    local choices = cost_data.choices
    table.sort(choices)
    local list = {}
    for i = 1, 4, 1 do
      table.insert(list, "#lb_zero__shouwu_" .. i)
    end
    room:setPlayerMark(player, "@[lb_zero__shouwu]lb_zero__shouwu-round", table.map(choices, function(choice)
      return table.indexOf(list, choice)
    end))
    room:setPlayerMark(player, "lb_zero__shouwu-round", 0)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if can_shouwu(player, 1) then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw and #move.moveInfo == 1 then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    use_shouwu(player, 1)
  end,
})

skel:addEffect(fk.Damage, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and can_shouwu(player, 2) and data.damage == 1
  end,
  on_use = function(self, event, target, player, data)
    use_shouwu(player, 2)
  end,
})

skel:addEffect(fk.HpRecover, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and can_shouwu(player, 3) and data.num == 1
  end,
  on_use = function(self, event, target, player, data)
    use_shouwu(player, 3)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if can_shouwu(player, 4) then
      for _, move in ipairs(data) do
        if move.proposer == player and move.moveReason == fk.ReasonDiscard and #move.moveInfo == 1
            and (move.moveInfo[1].fromArea == Card.PlayerEquip or move.moveInfo[1].fromArea == Card.PlayerJudge) then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    use_shouwu(player, 4)
  end,
})

skel:addEffect(fk.RoundEnd, {
  audio_index = { 3, 4 },
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and #player:getTableMark("@[lb_zero__shouwu]lb_zero__shouwu-round") == #player:getTableMark("lb_zero__shouwu-round")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local list = player:getTableMark("lb_zero__shouwu-round")
    table.sort(list)
    for _, choice in ipairs(list) do
      if player.dead then return end
      if choice == 1 then
        player:drawCards(1, skel.name)
      elseif choice == 2 then
        local targets = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          targets = room.alive_players,
          skill_name = skel.name,
          prompt = "#lb_zero__shouwu_damage",
          cancelable = false,
        })
        if #targets > 0 then
          room:damage {
            from = player,
            to = targets[1],
            skillName = skel.name,
            damage = 1,
          }
        end
      elseif choice == 3 then
        room:recover {
          who = player,
          num = 1,
          skillName = skel.name,
          recoverBy = player,
        }
      elseif choice == 4 then
        local targets = table.filter(room.alive_players, function(p)
          return #p:getCardIds("ej") > 0
        end)
        if #targets > 1 then
          targets = room:askToChoosePlayers(player, {
            min_num = 1,
            max_num = 1,
            targets = targets,
            skill_name = skel.name,
            prompt = "#lb_zero__shouwu_choose",
            cancelable = false,
          })
        end
        if #targets > 0 then
          local to = targets[1]
          local card = room:askToChooseCard(player, {
            target = to,
            flag = "ej",
            skill_name = skel.name,
            prompt = "#lb_zero__shouwu_dis:" .. to.id
          })
          room:throwCard(card, skel.name, to, player)
        end
      end
    end
  end,
})

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[lb_zero__shouwu]lb_zero__shouwu-round", 0)
  player.room:setPlayerMark(player, "lb_zero__shouwu-round", 0)
end)

return skel
